package com.yzjs.common.base.service;

import com.yzjs.common.base.entity.BasePojo;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.Resource;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.math.BigInteger;
import java.util.LinkedList;
import java.util.List;

public class BaseServiceImpl<T extends BasePojo,D>  {


    private Class<T> clazzt  ;

    private Class<D> clazzd;

   private String scanInfo1="com.yzjs.default.dao" ;

    private String scanInfo2="com.yzjs.localhost.dao" ;


    public BaseServiceImpl() {
        Type genType = getClass().getGenericSuperclass();
        Type[] params = ((ParameterizedType) genType).getActualTypeArguments();
        clazzt = (Class) params[0];
        clazzd = (Class) params[1];
    }

//    @Resource(name="data1SqlSessionFactory")
//    private SqlSessionFactory data1SqlSessionFactory;

    @Resource(name="data2SqlSessionFactory")
    private SqlSessionFactory data2SqlSessionFactory;

    public T findOne(T t){
        SqlSession sqlSession =  null;
        if (clazzd.getName().contains(scanInfo1)){
         ///   sqlSession  = data1SqlSessionFactory.openSession();
        }else{
            sqlSession = data2SqlSessionFactory.openSession();
        }

        return sqlSession.selectOne(clazzd.getName()+".findOne",t);
    }

    public List<T> findList(T t){
        SqlSession sqlSession =  null;
        if (clazzd.getName().contains(scanInfo1)){
          //  sqlSession  = data1SqlSessionFactory.openSession();
        }else{
            sqlSession = data2SqlSessionFactory.openSession();
        }
        return sqlSession.selectList(clazzd.getName()+".findList",t);
    }


    public int insert(T t  ){
        SqlSession sqlSession =  null;
        if (clazzd.getName().contains(scanInfo1)){
         //   sqlSession  = data1SqlSessionFactory.openSession();
        }else{
            sqlSession = data2SqlSessionFactory.openSession();
        }
        return sqlSession.insert(clazzd.getName()+".insert",t);

    }



    /**
     * 批量插入
     * @param ts
     * @return
     */
    public List<Integer> insertBatch(List<T> ts ){
        SqlSession sqlSession =  null;
        if (clazzd.getName().contains(scanInfo1)){
           // sqlSession  = data1SqlSessionFactory.openSession();
        }else{
            sqlSession = data2SqlSessionFactory.openSession();
        }
        sqlSession.insert(clazzd.getName()+".insertBatch",ts);

        List<Integer> ids =  new LinkedList<>();
        for(T t : ts ){
          int  id = t.getId() == null?1:((BigInteger) t.getId()).intValue();
          ids.add(id);
        }
        return ids;
    }


    public int update(T t){
        SqlSession sqlSession =  null;
        if (clazzd.getName().contains(scanInfo1)){
        //    sqlSession  = data1SqlSessionFactory.openSession();
        }else{
            sqlSession = data2SqlSessionFactory.openSession();
        }
        return sqlSession.update(clazzd.getName()+".update",t);
    }
    public int delete(T t){
        SqlSession sqlSession =  null;
        if (clazzd.getName().contains(scanInfo1)){
          //  sqlSession  = data1SqlSessionFactory.openSession();
        }else{
            sqlSession = data2SqlSessionFactory.openSession();
        }
        return sqlSession.update(clazzd.getName()+".delete",t);
    }
}
